package org.tity.sql.dao;

import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.tity.sql.code.entity.test.TestNewsTity;
import org.tity.sql.code.entity.test.RelationTity;
import org.tity.sql.dao.base.TableDataTest;
import org.tity.sql.data.Rcd;
import org.tity.sql.data.RcdSet;
import org.tity.sql.data.SaveMode;

 /**
  * 针对各种值类型测测试
  * */
public class DAO_Entity  extends TableDataTest {
 
	@Test
	public void test_insert_update() {
		 TestNewsTity news=new TestNewsTity();
		 news.setCode("AA");
		 news.setTitle("测试标题");
		 if(!dao.getTableColumnMeta(normalTable, "id").isAutoIncrease()) {
			 news.setId(10);
		 }
		 
		 //插入测试
		 boolean suc=dao.insertEntity(news);
		 assertTrue(suc);
		 
		 if(dao instanceof MySqlDAO)
		 {
			 assertTrue(news.getId()!=null);
			 assertTrue(news.getId()>0);
		 }
		 
		 RcdSet rs=dao.query("select * from "+normalTable+" where code=?","AA");
		 assertTrue(rs.size()==1);
		 assertTrue("测试标题".equals(rs.getRcd(0).getString("title")));
		 
		 //转换为实体
		 TestNewsTity news2=rs.getRcd(0).toEntity(TestNewsTity.class);
		 assertTrue("测试标题".equals(news2.getTitle()));
		 assertTrue("AA".equals(news2.getCode()));
		 
		 //更新
		 news2.setNewsid("today");
		 suc=dao.updateEntity(news2,SaveMode.NOT_NULL_FIELDS);
		 assertTrue(suc);
		 rs=dao.query("select * from "+normalTable+" where code=?","AA");
		 assertTrue(rs.size()==1);
		 assertTrue("测试标题".equals(rs.getRcd(0).getString("title")));
		 assertTrue("today".equals(rs.getRcd(0).getString("newsId")));
 
	}
	
	@Test
	public void test_save() {
		TestNewsTity news=new TestNewsTity();
		 news.setCode("AA");
		 news.setTitle("测试标题");
		 if(!dao.getTableColumnMeta(normalTable, "id").isAutoIncrease()) {
			 news.setId(10);
		 }
		 //插入测试
		 boolean suc=dao.saveEntity(news, SaveMode.NOT_NULL_FIELDS);
		 assertTrue(suc);
		 assertTrue(news.getId()!=null);
		 
		 news.setTitle("T2");
		 news.setNewsid("NS");
		 suc=dao.saveEntity(news,SaveMode.NOT_NULL_FIELDS);
		 assertTrue(suc);
		 
		 RcdSet rs=dao.query("select * from "+normalTable+" where code=?","AA");
		 assertTrue(rs.size()==1);
		 TestNewsTity newsdb=rs.getRcd(0).toEntity(TestNewsTity.class);
		 assertTrue("T2".equals(newsdb.getTitle()));
		 assertTrue("NS".equals(newsdb.getNewsid()));
		 
		 
	}
	
	
	@Test
	public void test_delete_by_pk() {
		 TestNewsTity news=new TestNewsTity();
		 news.setCode("AA");
		 news.setTitle("测试标题");
		 if(!dao.getTableColumnMeta(normalTable, "id").isAutoIncrease()) {
			 news.setId(10);
		 }
		 //插入测试
		 boolean suc=dao.insertEntity(news);
		 assertTrue(suc);
		 if(dao instanceof MySqlDAO)
		 {
			 assertTrue(news.getId()!=null);
		 }
		 
		 //删除
		 suc=dao.deleteEntityByPK(news);
		 assertTrue(suc);
		 
		 RcdSet rs=dao.query("select * from "+normalTable+" where code=?","AA");
		 assertTrue(rs.size()==0);
	}
	
	
	@Test
	public void test_delete_by_sample() {
		TestNewsTity news=new TestNewsTity();
		 news.setCode("AA");
		 news.setTitle("测试标题");
		 if(!dao.getTableColumnMeta(normalTable, "id").isAutoIncrease()) {
			 news.setId(10);
		 }
		 //插入测试
		 boolean suc=dao.insertEntity(news);
		 assertTrue(suc);
		 
		 //删除
		 TestNewsTity sample=new TestNewsTity();
		 sample.setCode("AA");
		 
		 int i=dao.deleteEntitys(sample);
		 assertTrue(i==1);
		 
		 RcdSet rs=dao.query("select * from "+normalTable+" where code=?","AA");
		 assertTrue(rs.size()==0);
	}
	
	
	
	@Test
	public void test_pk_table() {
		
		 RelationTity rel=new RelationTity();
		 rel.setBillId(9);
		 rel.setOwnerId(1);
		 rel.setType("shop");
		  
		 //插入测试
		 boolean suc=dao.insertEntity(rel);
		 assertTrue(suc);
		 
		 
		 rel.setBillId(19);
		 rel.setOwnerId(99);
		 suc=dao.saveEntity(rel, SaveMode.DIRTY_FIELDS);
		 assertTrue(suc);
		 
		 RcdSet rs0=dao.query("select * from "+pkTable);
		 assertTrue(rs0.size()==1);
		 Rcd r=rs0.getRcd(0);
		 assertTrue(r.getInteger("bill_id")==19);
		 assertTrue(r.getInteger("owner_id")==99);
		 
		 //删除
		 suc=dao.deleteEntityByPK(rel);
		 assertTrue(suc);
		 
		 RcdSet rs=dao.query("select * from "+pkTable);
		 assertTrue(rs.size()==0);
		 
		
	}
 
}
